package org.linlinjava.litemall.db.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.db.dao.SecKillMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class SeckillService {

    private final SecKillMapper secKillMapper;

    public Map<String, List<LitemallSeckill>> secKillAll() {
        // 获取开始时间是今日的
        List<LitemallSeckill> secKills = secKillMapper.selectList(Wrappers.<LitemallSeckill>lambdaQuery()
                .le(LitemallSeckill::getStartTime, DateUtil.currentTime())
                .ge(LitemallSeckill::getEndTime, DateUtil.currentTime()));
        return secKills.stream().collect(Collectors.groupingBy(s -> s.getStartTime() + "_" + s.getEndTime(), Collectors.toList()));
    }

    public List<LitemallSeckill> secKillNew() {
        // 获取开始时间是今日的
        List<LitemallSeckill> secKills = secKillMapper.selectList(Wrappers.<LitemallSeckill>lambdaQuery()
                .le(LitemallSeckill::getStartTime, DateUtil.currentTime())
                .ge(LitemallSeckill::getEndTime, DateUtil.currentTime()));
        if (secKills.isEmpty()){
            return new ArrayList<LitemallSeckill>();
        }
        Map<String, List<LitemallSeckill>> collect = secKills.stream().collect(Collectors.groupingBy(s -> s.getStartTime() + "_" + s.getEndTime(), Collectors.toList()));
        // 获取最新时间数据
        return collect.get(Collections.min(collect.keySet()));
    }

    public List<LitemallSeckill> querySelective(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        QueryWrapper<LitemallSeckill> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.orderByDesc("sort_order");
        return secKillMapper.selectList(objectQueryWrapper);
    }

    public void add(LitemallSeckill seckill) {
        seckill.setUpdateTime(LocalDateTime.now());
        secKillMapper.insert(seckill);
    }

    public LitemallSeckill findById(Integer id) {
        return secKillMapper.selectById(id);
    }

    public LitemallSeckill findByGoodIdBetween(Integer goodId) {
        return secKillMapper.selectOne(Wrappers.<LitemallSeckill>lambdaQuery()
                .eq(LitemallSeckill::getGoodId, goodId)
                .gt(LitemallSeckill::getEndTime, DateUtil.currentTime())
                .le(LitemallSeckill::getStartTime, DateUtil.currentZero()));
    }

    public LitemallSeckill findByGoodGe(Integer goodId) {
        QueryWrapper<LitemallSeckill> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("good_id", goodId);
        objectQueryWrapper.ge("start_time", DateUtil.currentTime());
        return secKillMapper.selectOne(objectQueryWrapper);
    }

    public int updateById(LitemallSeckill seckill) {
        seckill.setUpdateTime(LocalDateTime.now());
        return secKillMapper.updateById(seckill);
    }

    public void deleteById(Integer id) {
        secKillMapper.deleteById(id);
    }

    public int reduceStock(Integer id, Short number) {
        return secKillMapper.reduceStock(id, number);
    }
}